from selenium.common.exceptions import TimeoutException, NoSuchElementException
from Base.base import Base
from Page.app.Page_elements import PageElements


class AppChoiceHome(Base):
    def __init__(self):
        super().__init__("app")

    def choice_channel(self, name):
        """
        banner选择渠道
        :param name: 频道名字
        :return:
        """
        # 定位banner框
        banner = self.search_ele(PageElements.app_banner_area)
        # 取banner左上角x轴
        x = banner.location.get("x")
        # 取banner左上角y轴
        y = banner.location.get("y")

        # 取banner宽
        width = banner.size.get("width")
        # 取banner高
        height = banner.size.get("height")
        # 死循环
        while True:
            page_ele = self.driver.page_source
            try:
                # 定位频道名字
                self.search_click(
                    (PageElements.app_banner_option[0], PageElements.app_banner_option[1].format(name)),timeout=3
                )
                break
            except TimeoutException:

                # 定位不到-向左滑动一次  起点：x+width*0.8,y+height*0.5   终点：x+width*0.2,y+height*0.5
                self.driver.swipe(x + width * 0.8, y + height * 0.5, x + width * 0.2, y + height * 0.5, 1500)
                # 判断页面末端
                if page_ele == self.driver.page_source:
                    # 到页面底端
                    raise NoSuchElementException("banner列表中没有找到文本：{}".format(name))
